package com.ganee.agora.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.ganee.agora.R
import com.ganee.agora.viewmodel.DetailViewModel
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.httprequest.utils.ToastUtil
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import com.ganee.baselib.widget.image.transform.GlideRoundTransform
import kotlinx.android.synthetic.main.activity_register_info.*
import kotlinx.android.synthetic.main.toolbar_with_title.*
import okhttp3.MediaType
import okhttp3.RequestBody
import org.json.JSONException
import org.json.JSONObject

@Route(path = Constance.ACTIVITY_URL_REGISTER_INFO)
class RegisterInfoActivity : BaseActivity<DetailViewModel>(), View.OnClickListener {

    @JvmField
    @Autowired(name = "activeId")
    var activeId: String? = null

    @JvmField
    @Autowired(name = "eventTitle")
    var eventTitle: String? = null

    @JvmField
    @Autowired(name = "host")
    var host: String? = null

    @JvmField
    @Autowired(name = "email")
    var email: String? = null

    @JvmField
    @Autowired(name = "time")
    var time: String? = null
    @JvmField
    @Autowired(name = "activePic")
    var activePic: String? = null

    @JvmField
    @Autowired(name = "tickets")
    var tickets: String? = null

    private var num:Int = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register_info)
        ARouter.getInstance().inject(this)
        initView()
    }

    private fun initView() {
        toolbarCancel.setOnClickListener{
            finish()
        }
        toolbarTitle.text = "Register"
        register_info_et_title.text = eventTitle ?: ""
        register_info_et_email.text = email ?: ""
        register_info_tv_title.text = title ?: ""
        register_info_tv_user.text = host ?: ""
        register_info_tv_time.text = time ?: ""

        Glide.with(this@RegisterInfoActivity)
                .load(activePic)
                .transform(GlideRoundTransform(this@RegisterInfoActivity))
                .placeholder(R.drawable.home_placeholder)
                .into(register_info_iv_pic)

        register_info_tv_number.text = num.toString()
        register_info_tv_amount.text = num.toString()
        register_info_tv_reduce.setOnClickListener(this)
        register_info_tv_add.setOnClickListener(this)
        register_info_tv_pay.setOnClickListener(this)
    }

    override fun getViewModel(): DetailViewModel {
        return ViewModelProviders.of(this@RegisterInfoActivity).get(DetailViewModel::class.java)
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.register_info_tv_reduce -> {
                if (num > 1){
                    num -= 1
                } else {
                    num = 1
                }
                register_info_tv_number.text = num.toString()
                register_info_tv_amount.text = num.toString()
            }
            R.id.register_info_tv_add -> {
                num += 1
                register_info_tv_number.text = num.toString()
                register_info_tv_amount.text = num.toString()
            }
            R.id.register_info_tv_pay -> {
                val requestData = JSONObject()
                try {
                    requestData.put("activeId", activeId)
                    requestData.put("tickets", tickets)
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                val requestBody = RequestBody.create(MediaType.parse("application/json"), requestData.toString())
                viewModel.postEnroll(requestBody)
                viewModel.enroll.observe(this, Observer {
                    this@RegisterInfoActivity.finish()
                    ToastUtil.showToast("Sign up success")
                })
            }
        }
    }
}
